Repository navigation
Treat unci pixel_size as an exact stride for sub-byte pixel groups - #25
Merged
Merged
Conversation
handle_pixel_alignment counted whole bytes and kept any leftover bit offset, so a 30-bit RGB group in a 4-byte pixel skipped one byte and started the next pixel at bit 38 instead of bit 32. A payload of pixel_size x width bytes could fail as truncated or decode to wrong pixels with no error, depending on the group size and row width. For 30-bit RGB in a 4-byte pixel, widths divisible by 4 decoded wrongly. Generically compressed items failed the same way after decompression, and for a 2x1 image the nine bytes the old reader expected also exceeded the eight-byte decompression budget. Track the pixel start in bits and advance to exactly pixel_size bytes past it. A group that does not fit pixel_size is now rejected even when it overflows by less than a byte, as libheif and GPAC already do. This deliberately differs from libheif's generic pixel path, which starts the next pixel at bit 30 because its byte index rounds a partial byte up. That path disagrees with libheif's own tile sizing, block pixel decoder and encoder, and with GPAC. No file in the libheif or local HEIC corpora uses such a layout, and decode output for libheif's 170 unci files is unchanged.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pixel_sizelayouts whose components don't fill whole bytes decoded wrongly, for example 10-bit RGB inpixel_size4 or 7-bit mono inpixel_size2. The reader counted whole bytes and kept the leftover bits, so after 30 bits it skipped one byte and started the next pixel at bit 38 instead of bit 32.A stride-sized payload could fail as truncated or decode to incorrect pixels, depending on the group size and row width. For 10-bit RGB in
pixel_size4, widths divisible by 4 decoded incorrectly, including 18 of 24 pixels on an 8×3 fixture. The compressed versions showed the same failures after decompression. For the 2×1 RGB10 example, the nine-byte payload expected by the old reader also exceeded the eight-byte decompression budget. Each pixel now starts exactlypixel_sizebytes after the previous one, including multi-Y groups. A group that doesn't fit is rejected even when it overflows by less than a byte, which matches libheif and GPAC.Diverging from libheif
This intentionally differs from libheif's generic pixel path, and that is the right call. The crate never matched that path on these layouts anyway. Here is a 2x1 image of 10-bit RGB in
pixel_size4, from an 8-byte payload:block_size4)libheif's byte index rounds a partial byte up, so after 30 bits it skips nothing and
pixel_sizehas no effect. Its ownget_tile_data_sizes, block pixel decoder and encoder all use an exactpixel_sizestride, so its generic reader is the odd one out. Tested at b97c8b5f and the pinned 78638f4; upstream master has the same code.Not checked against the spec text: the public preview of ISO/IEC 23001-17:2024 stops at 5.2.1.6.2, before the pixel-interleaving clause, 5.2.1.7 and the 5.2.3 semantics. The decision rests on the implementations above, so someone with the full text should confirm the
pixel_sizewording before this goes upstream.The
verifyjob's heif-dec comparison can't see this divergence. None of 651 local HEIF/HEIC/AVIF files has a sub-byte group withpixel_sizeset, and decode output for libheif's 170 unci files is byte-identical before and after.Source fork PR: laurens-pilot#13
@laurenspriem